/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package functions;

import model.*;
import functions.HealingSet;
import functions.ItemList;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author JArik
 */
public class OldUsers {

    public static HealingSet getOldVisits(int userId) throws SQLException
    {
        Database db = new Database();
        ResultSet set = db.getDBInfo(
                "SELECT OD.DAY2, OD.NIMI, OD.KOLIC, OD.CENA, OD.CENA1, OD.RCENA, OD.ARST, " +
                "       OV.VIZIT as visitPrice, OV.KLINIK as visitDescr, OV.DIAGN, OV.ODIAGN, OV.REKOM, " +
                "       OV.TUNUS, OV.ISXOD as result, OV.ROHTU as drugPrice, OV.LABOR as labPrice, " +
                "       OV.VAKT as vakcPrice, OV.USLUG as operPrice, OV.MUUD as otherPrice, OD.NOM " +
                     " FROM oldVisits OV LEFT JOIN oldDrugs OD ON " +
                     " OV.KARTA=OD.KARTA AND " +
                     " OV.DAY2=OD.DAY2 AND " +
                     " OV.NOM=OD.NOM " +
                     " WHERE OV.KARTA = (SELECT oldId FROM dogy.users WHERE id="+userId+") " +
                     " ORDER BY DAY2 DESC, NOM DESC");
        HealingSet healing = new HealingSet();
        ItemList<Item> visitDetails = new ItemList<Item>();
        Visit visit=null;
        Item item = null;
        int nom=-1;
        while(set.next())
        {
            if(nom == -1 ||
                    nom != set.getInt("NOM"))
            {
               visit = new Visit();
               nom = set.getInt("NOM");
               visitDetails = new ItemList<Item>();
               visit.setDisDate(set.getDate("DAY2"));
               visit.setPayed(set.getDouble("visitPrice"));
               visit.setDescr(set.getString("visitDescr"));
               visit.setRecom(set.getString("REKOM"));
               visit.setStatus(set.getString("TUNUS"));
               visit.setVisitOtherPayed(set.getDouble("otherPrice"));
               visit.setDoctor( new Doctor(0,"",set.getString("arst")));
               visitDetails.setDate(set.getDate("DAY2"));
               visitDetails.add(visit);
               String [] diags = null;
               if(set.getString("DIAGN")!= null)
                diags = set.getString("DIAGN").split(",");
               String odiags = set.getString("ODIAGN");
               if(diags != null)
               for(int i=0; i < diags.length; i++)
               {
                   Diagnose diag = new Diagnose();
                   diag.setName(diags[i]);
                   System.out.println(diags[i] + " " + odiags + " " + odiags.contains(diags[i]));
                   if(odiags.contains(diags[i]))
                       diag.setFinal();
                   visitDetails.add(diag);
               }
               healing.setResult(set.getString("result"));
               
               healing.addVisit(visitDetails);
            }
            item = new Item();
            item.setName(set.getString("NIMI"));
            item.setAmount(set.getDouble("KOLIC"));
            item.setSellPrices(set.getDouble("CENA"), set.getDouble("CENA1"), 
                    set.getDouble("RCENA"));
            item.setPayed(set.getDouble("RCENA"));
            visitDetails.add(item);
            
            
        }
        return healing;
    }

    public static boolean uniteVisits(int mainId, ArrayList<Integer> uniteIds)
                                                            throws SQLException
    {
        Database db = new Database();
        if(mainId <= 0)
            throw new SQLException("Выберите основного клиента");

        String users = " ( OV.KARTA=(SELECT oldId FROM users WHERE id="+uniteIds.get(0)+")";
        for(int i=1; i<uniteIds.size(); i++)
            users += " OR OV.KARTA=(SELECT oldId FROM users WHERE id="+uniteIds.get(i)+")";

        users += ") AND ( OD.KARTA=(SELECT oldId FROM users WHERE id="+uniteIds.get(0)+")";

        for(int i=1; i<uniteIds.size(); i++)
            users += " OR OD.KARTA=(SELECT oldId FROM users WHERE id="+uniteIds.get(i)+")";

        users +=")";

        String query = " UPDATE oldVisits OV, oldDrugs OD SET " +
                                  " OV.KARTA=(SELECT oldId FROM users WHERE id="+mainId+"), " +
                                  " OD.KARTA= (SELECT oldId FROM users WHERE id="+mainId+") " +
                                  " WHERE "+users;
        
        int res = db.updateDBInfo(query);
        
        db.closeStatement();
        //if(res == 1)
          return true;
        //else throw new SQLException("Ошибка, невозможно изменить информацию.");
    }

}
